package repository

import (
	"holidayOpenAPI/infrustructure/config"
	"holidayOpenAPI/infrustructure/domain"
	"holidayOpenAPI/infrustructure/model"
	"log"
	"time"
)

//FindHoliByID 通过id查找日期信息
func FindHoliByID(id int) interface{} {
	holiDate := model.HoliDate{}
	config.DB.Where("id = ?", id).First(&holiDate)
	if holiDate.ID == 0 {
		return nil
	}
	return holiDate
}

//FindHoliByDate 通过time寻找具体日期信息
func FindHoliByDate(time time.Time) model.HoliDate {
	holiDate := model.HoliDate{}
	config.DB.Where("year = ? and month = ? and day = ?", time.Year(), int(time.Month()), time.Day()).First(&holiDate)
	if holiDate.ID == 0 {
		var holidayType int
		var holiday bool
		var holidayName string
		if int(time.Weekday()) == 0 || int(time.Weekday()) == 6 {
			holidayType = 1
			holiday = true
			holidayName = "周末休息"
		} else {
			holidayType = 0
			holiday = false
			holidayName = "正常上班"
		}
		holiDate = model.HoliDate{
			DateTime:    time,
			Weekday:     int(time.Weekday()),
			Year:        time.Year(),
			Month:       int(time.Month()),
			Day:         time.Day(),
			Holiday:     holiday,
			HolidayName: holidayName,
			HolidayType: holidayType,
		}
	}
	return holiDate
}

//FindHoliByPage 分页查询节假日
func FindHoliByPage(page int, size int) []model.HoliDate {
	holidayArr := []model.HoliDate{}
	config.DB.Offset((page - 1) * size).Limit(size).Order("date_time desc").Find(&holidayArr)
	return holidayArr
}

//FindHoliBetweenTime 根据时间段查询节假日信息
func FindHoliBetweenTime(startTime time.Time, endTime time.Time) []model.HoliDate {
	holidayArr := []model.HoliDate{}
	config.DB.Where("date_time between ? and ?", startTime.String(), endTime).Find(&holidayArr)
	return holidayArr
}

//CountHoliDay 统计数据库中holiday的总条数
func CountHoliDay(startTime time.Time, endTime time.Time) int {
	var count int
	config.DB.Model(&model.HoliDate{}).Where("date_time between ? and ?", startTime, endTime).Count(&count)
	return count
}

//CreateHoliday 创建holiday
func CreateHoliday(do domain.HoliDateCreateDO) bool {
	dateTime, _ := time.Parse("2006-01-02", do.DateTime)
	holiDate := FindHoliByDate(dateTime)
	if holiDate.ID != 0 {
		log.Println("insert failed")
		return false
	}
	holiDate = model.HoliDate{
		DateTime:    dateTime,
		Weekday:     int(dateTime.Weekday()),
		Year:        dateTime.Year(),
		Month:       int(dateTime.Month()),
		Day:         dateTime.Day(),
		Holiday:     do.Holiday,
		HolidayName: do.HolidayName,
		HolidayType: do.HolidayType,
	}
	tx := config.DB.Begin()
	tx.Create(&holiDate)
	if result := tx.NewRecord(holiDate); result == false {
		log.Printf("insert successed，id is %d\n", holiDate.ID)
		tx.Commit()
		return true
	}
	tx.Rollback()
	log.Println("insert failed")
	return false
}

//UpdateHoliday 更新holiday
func UpdateHoliday(do domain.HoliDateUpdateDO) bool {
	holiDate := FindHoliByID(do.ID)
	if holiDate == nil {
		log.Println("update failed, id is incorrected")
		return false
	}
	dateTime, _ := time.Parse("2006-01-02", do.DateTime)
	holiDateByDate := FindHoliByDate(dateTime)
	if holiDateByDate.ID != 0 && holiDateByDate.ID != holiDate.(model.HoliDate).ID {
		log.Println("update failed")
		return false
	}

	holiDateUpdate := model.HoliDate{
		DateTime:    dateTime,
		Weekday:     int(dateTime.Weekday()),
		Year:        dateTime.Year(),
		Month:       int(dateTime.Month()),
		Day:         dateTime.Day(),
		Holiday:     do.Holiday,
		HolidayName: do.HolidayName,
		HolidayType: do.HolidayType,
	}
	holiDateUpdate.ID = uint(do.ID)
	tx := config.DB.Begin()
	tx.Save(&holiDateUpdate)
	tx.Commit()
	return true
}

//DeleteHoliday 删除holiday
func DeleteHoliday(id int) bool {
	holiDate := FindHoliByID(id)
	if holiDate == nil {
		log.Println("delete failed, id is incorrected")
		return false
	}
	tx := config.DB.Begin()
	tx.Where("id = ?", id).Delete(&model.HoliDate{})
	tx.Commit()
	return true
}
